//
//  DHHomeCell.m
//  宏达APP
//
//  Created by DIANGHONG on 2018/5/3.
//  Copyright © 2018年 123. All rights reserved.
//

#import "DHHomeCell.h"
#import "DHHomeButton.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "DHGoodListCell.h"

NSString *goodListCellID = @"goodListCellID";
@interface DHHomeCell()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic ,strong)NSArray<NSDictionary *> *buttonInfo;
@property(nonatomic ,strong)UIScrollView *scrollView;
@property(nonatomic ,strong)UICollectionView *goodlistView;
@property(nonatomic ,strong)UICollectionViewFlowLayout *flowLayout;
@end

@implementation DHHomeCell

#pragma mark: - 懒加载 UICollectionViewFlowLayout
- (UICollectionViewFlowLayout *)flowLayout{
    if (!_flowLayout) {
        //[self layoutIfNeeded];
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
       
    }
    return _flowLayout;
}
#pragma mark: - 懒加载 UICollectionView
- (UICollectionView *)goodlistView{
    if (!_goodlistView) {
       
        _goodlistView = [[UICollectionView alloc] initWithFrame:CGRectZero
                                           collectionViewLayout:self.flowLayout];
        _goodlistView.dataSource = self;
        _goodlistView.delegate = self;
    }
    return _goodlistView;
}
#pragma mark: - 懒加载 scrollView
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, self.bounds.size.height)];
    }
    return _scrollView;
}
#pragma mark: - 懒加载 首页按钮信息数组字典
- (NSArray *)buttonInfo{
    if (!_buttonInfo) {
        _buttonInfo = @[@{@"imageName":@"ico1",@"title":@"文案",@"clsName":@"DHOfficialController"},
                        @{@"imageName":@"ico5",@"title":@"热点"},
                        @{@"imageName":@"ico8",@"title":@"领卷"},
                        @{@"imageName":@"ico9",@"title":@"收藏"},
                        @{@"imageName":@"ico10",@"title":@"分类"}];
    }
    return _buttonInfo;
}

// 重写setDictData方法
- (void)setDictData:(NSDictionary *)dictData{
    _dictData = dictData;
    
    if ([dictData.allKeys.firstObject isEqualToString:@"adv_list"]){
        [self setupScrollViewWithDataArray:dictData[@"adv_list"][@"item"]];
    }
    if ([dictData.allKeys.firstObject isEqualToString:@"navigation"]) {
        [self creatHomeButton];
    }
    if ([dictData.allKeys.firstObject isEqualToString:@"home2"]) {
        [self setupHome2WithDataDict:dictData[@"home2"]];
    }
    if ([dictData.allKeys.firstObject isEqualToString:@"home1"]) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        [imgView sd_setImageWithURL:dictData[@"home1"][@"image"]];
    //    [imgView setContentMode:UIViewContentModeScaleAspectFit];

        [self.contentView addSubview:imgView];
        
    }
    if ([dictData.allKeys.firstObject isEqualToString:@"goods"]) {
        [self setupGoodListViewWithArrayData:dictData[@"goods"][@"item"]];
        
       // [self.goodlistView reloadData];
        
    }

}

// 实例化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
   // [self.goodlistView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellid"];
    [self.goodlistView registerNib:[UINib nibWithNibName:@"DHGoodListCell" bundle:nil] forCellWithReuseIdentifier:goodListCellID];
    return [super initWithStyle:style reuseIdentifier:reuseIdentifier];
}



#pragma mark: - 设置GoodListView
- (void)setupGoodListViewWithArrayData:(NSArray *)arrayData{
    
    NSInteger numOfRow = (arrayData.count % 2 == 0) ? arrayData.count / 2 : arrayData.count / 2 + 1;
    self.goodlistView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    self.flowLayout.itemSize = CGSizeMake(self.bounds.size.width / 2, self.bounds.size.height / numOfRow);
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.goodlistView.contentSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);

    self.goodlistView.showsVerticalScrollIndicator = false;
    self.goodlistView.showsHorizontalScrollIndicator = false;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

    [self.contentView addSubview:self.goodlistView];
    
    [self.goodlistView setBackgroundColor:UIColor.whiteColor];
    [self.goodlistView reloadData];

}


#pragma mark: - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return [self.dictData[@"goods"][@"item"] count];
    
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHGoodListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:goodListCellID forIndexPath:indexPath];
    
    if ([self.dictData.allKeys.firstObject isEqualToString:@"goods"]) {
        cell.goodData = self.dictData[@"goods"][@"item"][indexPath.row];
    }
    return cell;
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//
//}

#pragma mark: - 设置Home2
- (void)setupHome2WithDataDict:(NSDictionary *)dict{
    // iv1
    UIImageView *iv1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width / 2, self.bounds.size.height)];
    [iv1 setContentMode:UIViewContentModeScaleAspectFit];
    [iv1 sd_setImageWithURL:[NSURL URLWithString:dict[@"square_image"]]];
    [self.contentView addSubview:iv1];
    
    //iv2
    UIImageView *iv2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(iv1.frame), 0, self.bounds.size.width - iv1.bounds.size.width, self.bounds.size.height / 2)];
    [iv2 setContentMode:UIViewContentModeScaleAspectFit];
    [iv2 sd_setImageWithURL:[NSURL URLWithString:dict[@"rectangle1_image"]]];
    [self.contentView addSubview:iv2];
    
    //iv3
    UIImageView *iv3 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(iv1.frame), CGRectGetMaxY(iv2.frame),  iv2.bounds.size.width, iv2.bounds.size.height)];
    [iv3 setContentMode:UIViewContentModeScaleAspectFit];
    [iv3 sd_setImageWithURL:[NSURL URLWithString:dict[@"rectangle2_image"]]];
    [self.contentView addSubview:iv3];
    
}

#pragma mark: - 设置scrollView
- (void)setupScrollViewWithDataArray:(NSArray *)dataArray{
    
    [self.contentView addSubview:self.scrollView];
    
    for (NSDictionary *dict in dataArray) {
        NSString *imageUrl = dict[@"image"];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
        
        [self.scrollView addSubview:imageView];
    }
    
    //设置scrollView
    self.scrollView.bounces = false;
    self.scrollView.pagingEnabled = true;
    self.scrollView.contentSize = CGSizeMake(dataArray.count * self.bounds.size.width, 0);
    self.scrollView.showsVerticalScrollIndicator = false;
    
    self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
                                                                 attribute:NSLayoutAttributeLeft
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.contentView
                                                                 attribute:NSLayoutAttributeLeft
                                                                multiplier:1.0 constant:0]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
                                                                 attribute:NSLayoutAttributeRight
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.contentView
                                                                 attribute:NSLayoutAttributeRight
                                                                multiplier:1.0 constant:0]];
    
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
                                                                 attribute:NSLayoutAttributeTop
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.contentView
                                                                 attribute:NSLayoutAttributeTop
                                                                multiplier:1.0 constant:0]];
    
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
                                                                 attribute:NSLayoutAttributeBottom
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.contentView
                                                                 attribute:NSLayoutAttributeBottom
                                                                multiplier:1.0 constant:0]];
}


#pragma mark: - 循环遍历按钮数组字典 创建字典
- (void)creatHomeButton{
    CGFloat margin = 20;
    CGFloat btnW = ([UIScreen.mainScreen bounds].size.width - ((self.buttonInfo.count + 1) * margin)) / self.buttonInfo.count;
    
    [self.buttonInfo enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSString *imageName = obj[@"imageName"];
        NSString *title = obj[@"title"];
        NSString *clsName = obj[@"clsName"];
        
        DHHomeButton *btn = [DHHomeButton buttonWithImageName:imageName title:title];
        if (clsName) {
            btn.clsName = clsName;
        }
        [btn addTarget:self action:@selector(navigationButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.frame = CGRectMake((margin * (idx + 1)) + btnW * idx, 5, btnW, btnW);
        [self.contentView addSubview:btn];
    }];
}

#pragma mark: - 按钮点击事件监听
- (void)navigationButtonClick:(DHHomeButton *)selectedButton{
    
    DHHomeButton *btn = selectedButton;
    
    if (btn.clsName) {
       // NSLog(@"%@",btn.clsName);
        NSAssert(self.selectedButtonClick != nil, @"self.selectedButtonClick 不能为nil");
        
        self.selectedButtonClick(btn.clsName);
    }
}

@end
